{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "L9DtiP-0GIcj"
   },
   "source": [
    "# Iterative Prompt Develelopment\n",
    "In this lesson, you'll iteratively analyze and refine your prompts to generate marketing copy from a product fact sheet.\n",
    "\n",
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "GqmnSpjDGL83",
    "ExecuteTime": {
     "end_time": "2025-02-22T07:05:01.591658Z",
     "start_time": "2025-02-22T07:05:00.903407Z"
    }
   },
   "source": [
    "from openai import OpenAI\n",
    "import os\n",
    "\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv())\n"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-22T07:05:10.543227Z",
     "start_time": "2025-02-22T07:05:10.160587Z"
    }
   },
   "source": [
    "client = OpenAI(api_key=\"sk-7bec1e1708dd4d1abfbdd6f0238d3add\", base_url=\"https://api.deepseek.com\")"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "aP3PrqyzGOgc",
    "ExecuteTime": {
     "end_time": "2025-02-22T07:05:12.011161Z",
     "start_time": "2025-02-22T07:05:11.980807Z"
    }
   },
   "source": [
    "def get_completion(prompt, model=\"deepseek-chat\"):\n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]\n",
    "    response = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        temperature=0, # this is the degree of randomness of the model's output\n",
    "        stream=False\n",
    "    )\n",
    "    return response.choices[0].message.content"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "japrg7mAGR2g"
   },
   "source": [
    "## Generate a marketing product description from a product fact sheet"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "463Hski3GUF_",
    "ExecuteTime": {
     "end_time": "2025-02-22T07:05:21.984958Z",
     "start_time": "2025-02-22T07:05:21.968885Z"
    }
   },
   "source": [
    "fact_sheet_chair = \"\"\"\n",
    "OVERVIEW\n",
    "- Part of a beautiful family of mid-century inspired office furniture, \n",
    "including filing cabinets, desks, bookcases, meeting tables, and more.\n",
    "- Several options of shell color and base finishes.\n",
    "- Available with plastic back and front upholstery (SWC-100) \n",
    "or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n",
    "- Base finish options are: stainless steel, matte black, \n",
    "gloss white, or chrome.\n",
    "- Chair is available with or without armrests.\n",
    "- Suitable for home or business settings.\n",
    "- Qualified for contract use.\n",
    "\n",
    "CONSTRUCTION\n",
    "- 5-wheel plastic coated aluminum base.\n",
    "- Pneumatic chair adjust for easy raise/lower action.\n",
    "\n",
    "DIMENSIONS\n",
    "- WIDTH 53 CM | 20.87”\n",
    "- DEPTH 51 CM | 20.08”\n",
    "- HEIGHT 80 CM | 31.50”\n",
    "- SEAT HEIGHT 44 CM | 17.32”\n",
    "- SEAT DEPTH 41 CM | 16.14”\n",
    "\n",
    "OPTIONS\n",
    "- Soft or hard-floor caster options.\n",
    "- Two choices of seat foam densities: \n",
    " medium (1.8 lb/ft3) or high (2.8 lb/ft3)\n",
    "- Armless or 8 position PU armrests \n",
    "\n",
    "MATERIALS\n",
    "SHELL BASE GLIDER\n",
    "- Cast Aluminum with modified nylon PA6/PA66 coating.\n",
    "- Shell thickness: 10 mm.\n",
    "SEAT\n",
    "- HD36 foam\n",
    "\n",
    "COUNTRY OF ORIGIN\n",
    "- Italy\n",
    "\"\"\""
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "1GS_smYoGXPV"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**Product Description:**\n",
      "\n",
      "Elevate your workspace with the **Mid-Century Inspired Office Chair**, a stunning addition to our family of mid-century modern office furniture. Designed to blend timeless aesthetics with modern functionality, this chair is perfect for both home and business settings, offering unparalleled comfort and style.\n",
      "\n",
      "**Customizable to Your Taste:**  \n",
      "Choose from a variety of shell colors and base finishes, including stainless steel, matte black, gloss white, or chrome. Personalize your chair further with optional plastic or full upholstery, available in 10 fabric and 6 leather options. Whether you prefer armless design or 8-position PU armrests, this chair adapts to your needs.\n",
      "\n",
      "**Built for Comfort and Durability:**  \n",
      "Crafted with a 5-wheel plastic-coated aluminum base, this chair ensures smooth mobility on any surface. The pneumatic height adjustment allows for effortless customization, while the high-density HD36 foam seat provides exceptional support. Select from two seat foam densities—medium (1.8 lb/ft³) or high (2.8 lb/ft³)—to suit your comfort preferences.\n",
      "\n",
      "**Thoughtful Design Details:**  \n",
      "The chair features a durable cast aluminum shell with a modified nylon PA6/PA66 coating, ensuring long-lasting performance. With a sleek, mid-century silhouette and dimensions designed for ergonomic support (Width: 53 cm, Depth: 51 cm, Height: 80 cm), this chair is as practical as it is stylish.\n",
      "\n",
      "**Versatile and Reliable:**  \n",
      "Qualified for contract use, this chair is built to withstand the demands of high-traffic environments. Choose between soft or hard-floor casters to match your workspace needs.\n",
      "\n",
      "**Crafted in Italy:**  \n",
      "Proudly made in Italy, this chair combines expert craftsmanship with premium materials, delivering a product that’s as reliable as it is beautiful.\n",
      "\n",
      "Transform your office into a modern haven with the **Mid-Century Inspired Office Chair**—where elegance meets functionality.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "uQLhg4B5GZvZ"
   },
   "source": [
    "## Issue 1: The text is too long \n",
    "- Limit the number of words/sentences/characters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "ttK1xlwZGeBp"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elevate your workspace with this mid-century inspired office chair, blending style and functionality. Choose from a variety of shell colors, base finishes, and upholstery options. Featuring a 5-wheel base, pneumatic height adjustment, and customizable foam density, this Italian-crafted chair is perfect for home or business settings. Available with or without armrests.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "Use at most 50 words.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "wfBCEY7WGgKP"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(370, 51)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(response), len(response.split(' '))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "08PaEimyGibD"
   },
   "source": [
    "## Issue 2. Text focuses on the wrong details\n",
    "- Ask it to focus on the aspects that are relevant to the intended audience."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "id": "C3zpMAMmGkiv"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The mid-century inspired office chair features a durable cast aluminum shell with a modified nylon PA6/PA66 coating and a 5-wheel plastic-coated aluminum base. Available in multiple shell colors, base finishes, and upholstery options (fabric or leather). Includes HD36 foam seat, pneumatic height adjustment, and optional armrests. Made in Italy.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "The description is intended for furniture retailers, \n",
    "so should be technical in nature and focus on the \n",
    "materials the product is constructed from.\n",
    "\n",
    "Use at most 50 words.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "88bNVNwJGsiT"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The mid-century inspired office chair features a durable cast aluminum shell with a modified nylon PA6/PA66 coating and a 5-wheel plastic-coated aluminum base. Available in multiple shell colors, base finishes, and upholstery options (SWC-100, SWC-110), it offers customizable seat foam densities and armrest configurations. Made in Italy. Product IDs: SWC-100, SWC-110.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "The description is intended for furniture retailers, \n",
    "so should be technical in nature and focus on the \n",
    "materials the product is constructed from.\n",
    "\n",
    "At the end of the description, include every 7-character \n",
    "Product ID in the technical specification.\n",
    "\n",
    "Use at most 50 words.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zm1claQoGvGL"
   },
   "source": [
    "## Issue 3. Description needs a table of dimensions\n",
    "- Ask it to extract information and organize it in a table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "id": "0t88AoQNGx0H"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```html\n",
      "<div>\n",
      "  <p>Introducing our mid-century inspired office chair, a perfect blend of style and functionality. This chair is part of a sophisticated furniture collection that includes filing cabinets, desks, bookcases, and meeting tables. It is designed to complement both home and business settings, making it a versatile choice for any environment.</p>\n",
      "  \n",
      "  <p>The chair features a durable 5-wheel plastic-coated aluminum base, ensuring smooth mobility and stability. The pneumatic chair adjustment allows for easy height adjustments, providing optimal comfort for users of all sizes. The chair is available with or without armrests, offering flexibility to suit individual preferences.</p>\n",
      "  \n",
      "  <p>Constructed with high-quality materials, the chair's shell base glider is made from cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and a sleek finish. The seat is padded with HD36 foam, available in two densities: medium (1.8 lb/ft³) or high (2.8 lb/ft³), to cater to different comfort needs. The chair is available in several shell colors and base finishes, including stainless steel, matte black, gloss white, or chrome, allowing for customization to match any decor.</p>\n",
      "  \n",
      "  <p>Choose between plastic back and front upholstery (SWC-100) or full upholstery (SWC-110) in a variety of fabric and leather options. The chair also offers soft or hard-floor caster options, making it suitable for different flooring types. Whether you need a chair for contract use or personal comfort, this product is designed to meet your needs.</p>\n",
      "  \n",
      "  <p>Product IDs: SWC-100, SWC-110</p>\n",
      "</div>\n",
      "\n",
      "<h2>Product Dimensions</h2>\n",
      "<table>\n",
      "  <tr>\n",
      "    <th>Dimension</th>\n",
      "    <th>Measurement (inches)</th>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>Width</td>\n",
      "    <td>20.87\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>Depth</td>\n",
      "    <td>20.08\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>Height</td>\n",
      "    <td>31.50\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>Seat Height</td>\n",
      "    <td>17.32\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>Seat Depth</td>\n",
      "    <td>16.14\"</td>\n",
      "  </tr>\n",
      "</table>\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "The description is intended for furniture retailers, \n",
    "so should be technical in nature and focus on the \n",
    "materials the product is constructed from.\n",
    "\n",
    "At the end of the description, include every 7-character \n",
    "Product ID in the technical specification.\n",
    "\n",
    "After the description, include a table that gives the \n",
    "product's dimensions. The table should have two columns.\n",
    "In the first column include the name of the dimension. \n",
    "In the second column include the measurements in inches only.\n",
    "\n",
    "Give the table the title 'Product Dimensions'.\n",
    "\n",
    "Format everything as HTML that can be used in a website. \n",
    "Place the description in a <div> element.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Msdd_7s2G02l"
   },
   "source": [
    "## Load Python libraries to view HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "id": "as0ohsSAG4tQ"
   },
   "outputs": [],
   "source": [
    "from IPython.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "id": "Dl4E2C38G6yj"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "```html\n",
       "<div>\n",
       "  <p>Introducing our mid-century inspired office chair, a perfect blend of style and functionality. This chair is part of a sophisticated furniture collection that includes filing cabinets, desks, bookcases, and meeting tables. It is designed to complement both home and business settings, making it a versatile choice for any environment.</p>\n",
       "  \n",
       "  <p>The chair features a durable 5-wheel plastic-coated aluminum base, ensuring smooth mobility and stability. The pneumatic chair adjustment allows for easy height adjustments, providing optimal comfort for users of all sizes. The chair is available with or without armrests, offering flexibility to suit individual preferences.</p>\n",
       "  \n",
       "  <p>Constructed with high-quality materials, the chair's shell base glider is made from cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and a sleek finish. The seat is padded with HD36 foam, available in two densities: medium (1.8 lb/ft³) or high (2.8 lb/ft³), to cater to different comfort needs. The chair is available in several shell colors and base finishes, including stainless steel, matte black, gloss white, or chrome, allowing for customization to match any decor.</p>\n",
       "  \n",
       "  <p>Choose between plastic back and front upholstery (SWC-100) or full upholstery (SWC-110) in a variety of fabric and leather options. The chair also offers soft or hard-floor caster options, making it suitable for different flooring types. Whether you need a chair for contract use or personal comfort, this product is designed to meet your needs.</p>\n",
       "  \n",
       "  <p>Product IDs: SWC-100, SWC-110</p>\n",
       "</div>\n",
       "\n",
       "<h2>Product Dimensions</h2>\n",
       "<table>\n",
       "  <tr>\n",
       "    <th>Dimension</th>\n",
       "    <th>Measurement (inches)</th>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>Width</td>\n",
       "    <td>20.87\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>Depth</td>\n",
       "    <td>20.08\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>Height</td>\n",
       "    <td>31.50\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>Seat Height</td>\n",
       "    <td>17.32\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>Seat Depth</td>\n",
       "    <td>16.14\"</td>\n",
       "  </tr>\n",
       "</table>\n",
       "```"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(HTML(response))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ukXcw3I7G8xT"
   },
   "source": [
    "## Try experimenting on your own!"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyOQ2grJrTuuAVKosFYIQS8B",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "pytorch_py39_jupyter",
   "language": "python",
   "name": "pytorch_py39_jupyter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
